package com.yzf.wisecoder.controller;

import com.yzf.file.storage.core.StorageTemplate;
import com.yzf.wisecoder.wisecodercommon.response.Response;
import com.yzf.wisecoder.wisecodercommon.utils.ImgCompressUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

/**
 * @author jianghuiyong
 * @Description OSS控制
 * @date 2020/5/14 19:21
 */
@RestController
@RequestMapping("sys/oss")
@Api(value = "OSS控制", tags = "OSS控制")
@Slf4j
public class OssController {


    /**
     * 上传图片
     *
     * @param file
     * @return
     */
    @PostMapping("upload")
    @ApiOperation("上传文件")
    public Map upload(@RequestBody MultipartFile file) {
        String extName = file.getOriginalFilename().indexOf(".") == -1 ? "" : file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
        String fileName = file.getOriginalFilename().indexOf(".") == -1 ? file.getOriginalFilename() : file.getOriginalFilename().substring(0, file.getOriginalFilename().lastIndexOf("."));
        String fileKey = fileName + "." + System.currentTimeMillis() + extName;
        Map result = new HashMap(8);
        result.put("name", fileName);
        try {
            StorageTemplate.uploadInputStream(fileKey, file.getInputStream(), file.getContentType());
        } catch (IOException e) {
            log.info("上传文件失败:{}", e.toString());
            result.put("status", "error");
            return result;
        }
        String url = StorageTemplate.getDownloadUrl(fileKey);
        url = url.substring(0, url.indexOf("?"));
        result.put("status", "done");
        result.put("url", url);
        result.put("thumbUrl", url);
        return result;
    }

    /**
     *上传缩略图
     *
     * @param file
     * @return
     */
    @PostMapping("uploadCompress")
    @ApiOperation(value = "上传缩略图")
    public Map uploadCompress(@RequestBody MultipartFile file){
        String extName = file.getOriginalFilename().indexOf(".") == -1 ? "" : file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".") + 1);
        String fileName = file.getOriginalFilename().indexOf(".") == -1 ? file.getOriginalFilename() : file.getOriginalFilename().substring(0, file.getOriginalFilename().lastIndexOf("."));
        String fileKey = fileName + "." + System.currentTimeMillis() + "." + extName;
        Map result = new HashMap(8);
        result.put("name", fileName);
        try {
            BufferedImage bufferedImage = ImgCompressUtil.resize(file.getInputStream(), 320, 180);
            InputStream inputStream = ImgCompressUtil.getImageStream(bufferedImage, extName);
            StorageTemplate.uploadInputStream(fileKey, inputStream, file.getContentType());
        } catch (IOException e) {
            log.info("上传文件失败:{}", e.toString());
            result.put("status", "error");
            return result;
        }
        String url = StorageTemplate.getDownloadUrl(fileKey);
        url = url.substring(0, url.indexOf("?"));
        result.put("status", "done");
        result.put("url", url);
        result.put("thumbUrl", url);
        return result;
    }


}
